package com.zkingsoft.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.BizCoupon;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.dao.BizCouponDao;
import com.zkingsoft.service.BizCouponService;

/**
 * @description service接口实现类(优惠券)
 * @author 何明
 * @date 2018-05-23 00:08
 */
@Service
public class  BizCouponServiceImpl implements  BizCouponService{

	
	@Autowired
	private BizCouponDao bizCouponDao;
	
	
	@Override
	public int add(BizCoupon bizCoupon){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizCoupon.setCreateBy(loginUser.getSuName());
		bizCoupon.setUpdateBy(loginUser.getSuName());
		bizCoupon.setCouId(UUIDUtil.getRandomID());
		return bizCouponDao.insert(bizCoupon);
		
	}
	
	@Override
	public int batchAdd(List<BizCoupon>  bizCouponList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizCouponList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizCouponList.size() ? bizCouponList.size() : end;
			List<BizCoupon> insertList = bizCouponList.subList(begin, end);
			num += bizCouponDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizCoupon oldBizCoupon
	,BizCoupon newBizCoupon){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizCoupon, newBizCoupon)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizCoupon, newBizCoupon);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizCoupon);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("couId", oldBizCoupon.getCouId());
			bizCouponDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizCoupon bizCoupon){
	
		return bizCouponDao.updateByModel(bizCoupon);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizCouponDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String couId){
	
		return bizCouponDao.deleteById(couId);
	
	}
	
	@Override
	public int removeByModel(BizCoupon bizCoupon){
	
		return bizCouponDao.deleteByModel(bizCoupon);
	
	}
	
	
	@Override
	public List<BizCoupon> findInPage(BizCoupon bizCoupon,  PaginationVO pageVo){
	
		return bizCouponDao.selectInPage(bizCoupon , pageVo);
	
	}
	
	@Override
	public List<BizCoupon> findByModel(BizCoupon bizCoupon){
	
		return bizCouponDao.selectByModel(bizCoupon);
	
	}
	
	@Override
	public int  findTotal(BizCoupon bizCoupon){
	
		return bizCouponDao.selectTotalRecord(bizCoupon);
	
	}
	
	@Override
	public BizCoupon  findById(String couId){
	
		return bizCouponDao.selectById(couId);
	
	}

   	
	
	
}